package com.hollycrm.unicom.sheet.generalCode.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.type.Type;

import com.hollycrm.springframework.exception.NestableRuntimeException;
import com.hollycrm.unicom.sheet.generalCode.hibernate.GeneralCodeRms;

public class GeneralCodeCRUDPoster extends HibernateDaoSupport {
	/* (non-Javadoc)
	 * @see com.hollycrm.unicom.encaps.crud.hibernate.LightweightCRUDPoster#create(net.sf.hibernate.Session, java.lang.Object)
	 */

	public void preCreate(final Object object) {

		getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

			String strCodeType = ((GeneralCodeRms) object).getCompID().getCodeType();
			String strCodeName = ((GeneralCodeRms) object).getCodeName();
			StringBuffer ql_codeCnt = new StringBuffer(
					"select count(o.rowID) from o in ").append(GeneralCodeRms.class).append(
					" where o.compID.codeType=? and o.codeName='"+strCodeName+"'");
			//System.out.println("ql_codeCnt:"+ql_codeCnt.toString());
			int agentCnt = ((Integer) ((List) session.find(ql_codeCnt
					.toString(), strCodeType, Hibernate.STRING)).get(0))
					.intValue();
			if (agentCnt > 0)
				throw new NestableRuntimeException(
						"error.crud.create.generalcodeRepeat");

//		 TODO Auto-generated method stub
		return null;
	}

});
	}
	public void preUpdate(final Serializable pk, final Object object) {

		getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

			String strCodeType = ((GeneralCodeRms) object).getCompID().getCodeType();
			String strCodeName = ((GeneralCodeRms) object).getCodeName();

			StringBuffer ql_codeCnt = new StringBuffer(
				"from o in ").append(GeneralCodeRms.class).append(
				" where o.compID.codeType=? and o.codeName=?");
			List list = (List) session.find(ql_codeCnt
					.toString(), new Object[]{strCodeType,strCodeName}, new Type[] {Hibernate.STRING,Hibernate.STRING});

			if (!list.isEmpty()){
				GeneralCodeRms f = (GeneralCodeRms) list.get(0);
				GeneralCodeRms l = (GeneralCodeRms) session.load(GeneralCodeRms.class,((GeneralCodeRms) object).getCompID());

				if(!f.getCompID().equals(l.getCompID())){
					throw new NestableRuntimeException(
					"error.crud.create.generalcodeRepeat");
				}
			}
//		 TODO Auto-generated method stub
		return null;
	}

});
	}

	/* (non-Javadoc)
	 * @see com.hollycrm.unicom.encaps.crud.hibernate.LightweightCRUDPoster#create(net.sf.hibernate.Session, java.lang.Object)
	 */
/*	public void postCreate(Object object) {
		try {
			String strWorkCode = ((Human) object).getWorkCode();
			StringBuffer ql_humanCnt = new StringBuffer(
					"select count(o) from o in ").append(Human.class).append(
					" where o.workCode=?");
			int agentCnt = ((Integer) ((List) session.find(ql_humanCnt
					.toString(), strWorkCode, Hibernate.STRING)).get(0))
					.intValue();
			if (agentCnt > 1)
				throw new NestableRuntimeException(
						"error.crud.create.humanRepeat");
		} catch (NestableRuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}*/


}
